package com.souku.finance.application.ui;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseTitleBarActivity;
import com.souku.finance.application.bean.VersionBean;
import com.souku.finance.application.http.retrofit.BaseDownloadCallback;
import com.souku.finance.application.http.retrofit.RetrofitUtils;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.level.AppInstall;
import com.souku.finance.application.utils.level.SystemUtils;
import com.souku.finance.application.utils.level.UIUtils;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class VersionNewActivity extends BaseTitleBarActivity {

    @BindView(R.id.iv_bg)
    ImageView ivBg;
    @BindView(R.id.bt_upgrade)
    Button btUpgrade;
    @BindView(R.id.tv_line_1)
    TextView tvLine1;
    @BindView(R.id.tv_line_2)
    TextView tvLine2;
    @BindView(R.id.tv_line_3)
    TextView tvLine3;
    @BindView(R.id.iv_close)
    ImageView ivClose;

    VersionBean versionBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_version_new);
        ButterKnife.bind(this);
        getNewVersion();
    }

    @Override
    public void internet() {

    }

    public void getNewVersion() {
        RetrofitUtils.create().getNewVersion().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String json = response.body().string();
                    if (!TextUtils.isEmpty(json)) {
                        versionBean = JsonUtil.fastBean(JsonUtil.getData(json).toString(), VersionBean.class);
                        for (int i = 0; i < versionBean.getContent().size(); i++) {
                            if (i == 0) {
                                tvLine1.setText(versionBean.getContent().get(i));
                            } else if (i == 1) {
                                tvLine2.setText(versionBean.getContent().get(i));
                            } else if (i == 2) {
                                tvLine3.setText(versionBean.getContent().get(i));
                            }
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    @OnClick({R.id.iv_close, R.id.bt_upgrade})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                onBackPressed();
                break;
            case R.id.bt_upgrade:
                if (versionBean != null) {
                    upgradeApp();
                    MobclickAgent.onEvent(VersionNewActivity.this, "updateApp");
                }
                break;
        }
    }

    public void upgradeApp() {
        String currentVersionCode = SystemUtils.getVersionCode(VersionNewActivity.this);
        if (versionBean.getVersionCode() > Integer.parseInt(currentVersionCode)) {
            //有新版本
            UIUtils.showToastSafe("请稍等,后台正在下载..");
            Call<ResponseBody> call = RetrofitUtils.create().getApp();
            call.enqueue(new BaseDownloadCallback() {
                @Override
                public void onSuccess(String path) {
                    AppInstall.install(VersionNewActivity.this, path);
                }

                @Override
                public void onError(Call<ResponseBody> call) {

                }
            });
            finish();
        }
    }


}
